﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Lawyer.Web
{
    public partial class customupdate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }
        private string InitFromUrl(string url)
        {
            string rtn = Request[url] != null ? Request[url] : "";
            return rtn;
        }
        private void BindData()
        {
            string ID = InitFromUrl("ID");
            Lawyer.BLL.CustomInfo BLL = new Lawyer.BLL.CustomInfo();
            Lawyer.Model.CustomInfo model = BLL.GetModel(ID);
            if (model == null)
            {
                Response.Redirect("customlist.aspx");
            }

            txbBirthday.Text = model.Birthday.ToShortDateString();
            txbEmail.Text = model.Email;
            txbFullName.Text = model.FullName;
            txtAddress.Text = model.Address;
            txtAvatarURL.Text = model.AvatarURL;
            txtCity.Text = model.City;
            txtFax.Text = model.Fax;
            txtTelphone.Text = model.Telphone;
            rblGender.SelectedValue = model.Sex;

        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            Lawyer.Model.CustomInfo model = CreateFromForm();
            model.CustomID = InitFromUrl("ID");
            Lawyer.BLL.CustomInfo bll = new Lawyer.BLL.CustomInfo();
            if (bll.Update(model))
            {
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "success！", "customlist.aspx");
            }
            else
            {
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "failed！", "customadd.aspx");
            }



        }
        private Lawyer.Model.CustomInfo CreateFromForm()
        {

            string FullName = this.txbFullName.Text;
            string Email = this.txbEmail.Text;
            string Sex = this.rblGender.SelectedValue;
            DateTime Birthday = DateTime.Parse(this.txbBirthday.Text);
            string City = this.txtCity.Text;
            string Telphone = this.txtTelphone.Text;
            string Fax = this.txtFax.Text;
            string Address = this.txtAddress.Text;
            string AvatarURL = this.txtAvatarURL.Text;


            Lawyer.Model.CustomInfo model = new Lawyer.Model.CustomInfo();
            model.FullName = FullName;
            model.Email = Email;
            model.Sex = Sex;
            model.Birthday = Birthday;
            model.City = City;
            model.Telphone = Telphone;
            model.Fax = Fax;
            model.Address = Address;
            model.AvatarURL = AvatarURL;

            model.Status = 0;//0=>Normal,1=>Deleted

            model.UpdatedBy = "-1";
            model.UpdatedDate = DateTime.Now;
            return model;

        }
    }
}
